# -*- coding: utf-8 -*-
# filename: weiyi.py 
import urllib.request, urllib.parse, urllib.error,urllib.request,urllib.error,urllib.parse
import json


def hcapi(para,mmpid, relationId = None,cmdty=True):
    url = 'http://gfapi.vephp.com/hcapi'
    if relationId: #存在rid
        textmod = {'vekey':'V00001220Y06069594','detail':'1','para':para, 'pid':mmpid,'relationId':relationId}
    else:
        textmod = {'vekey':'V00001220Y06069594','detail':'1','para':para, 'pid':mmpid}
    textmod = urllib.parse.urlencode(textmod)
    req = urllib.request.Request(url = '%s%s%s' % (url,'?',textmod))
    res = urllib.request.urlopen(req).read()
    json_res = json.loads(res)
    if cmdty:
        return commodity(json_res)
    else:
        return json_res

def publishertkl(rtag):#会员渠道备案
    url = 'http://gfapi.vephp.com/publishertkl'
    textmod = {'vekey':'V00001220Y06069594', 'inviter_code':'RJXRMQ', 'rtag':rtag, 'text':'不得鸟会员备案'} # rtag = openid
    textmod = urllib.parse.urlencode(textmod)
    req = urllib.request.Request(url = '%s%s%s'%(url,'?',textmod))
    res = urllib.request.urlopen(req).read()
    json_res = json.loads(res)
    return json_res

def publisherget(relationId = None,page=1,pagesize=10):#轮讯渠道信息
    url = 'http://gfapi.vephp.com/publisherget'
    if relationId:
        textmod = {'vekey':'V00001220Y06069594', 'relationId':relationId, 'page':page, 'pagesize':pagesize}
    else:
        textmod = {'vekey':'V00001220Y06069594', 'page':page, 'pagesize':pagesize}

    textmod = urllib.parse.urlencode(textmod)
    req = urllib.request.Request(url='%s%s%s'%(url,'?',textmod))
    res = urllib.request.urlopen(req).read()
    json_res = json.loads(res) 
    return json_res

def tbkorder(start_time, order_scene=1):#订单查询 20分钟订单
    url = 'http://gfapiorder.vephp.com/order'
    textmod = {'vekey':'V00001220Y06069594', 'start_time':start_time, 'order_scene':order_scene, 'span':60}
    textmod = urllib.parse.urlencode(textmod)
    req = urllib.request.Request(url = '%s%s%s'%(url, '?', textmod))
    res = urllib.request.urlopen(req).read()
    json_res = json.loads(res)
    if json_res['data'] and json_res['error'] == '0':
        return json_res['data']
    else:
        return json_res['msg']

def products(topcat='热销', subcate='女装', page=1, pagesize=10, sort='tk_rate_des'):#热销高佣商品
    url = 'http://tmp.vephp.com/products'
    textmod = {
        'vekey':'V00001220Y06069594',
        'topcat':topcat,
        'subcate':subcate,
        'page':page,
        'pagesize':pagesize,
        'sort':sort
    }
    textmod = urllib.parse.urlencode(textmod)
    req = urllib.request.Request(url = '%s%s%s'%(url, '?', textmod))
    res = urllib.request.urlopen(req).read()
    json_res = json.loads(res)
    return json_res

class order(object):
    def __init__(self, order_res):
        self.trade_parent_id = order_res['trade_parent_id']
        self.trade_id = order_res['trade_id']
        self.num_iid = order_res['num_iid']
        self.item_title = order_res['item_title']
        self.item_num = order_res['item_num']
        self.price = order_res['price']
        self.pay_price = order_res['pay_price']
        self.seller_nick = order_res['seller_nick']
        self.seller_shop_title = order_res['seller_shop_title']
        self.commission = order_res['commission']
        self.commission_rate = order_res['commission_rate']
        self.create_time = order_res['create_time']
        if 'earning_time' in order_res:
            self.earning_time = order_res['earning_time']
        self.tk_status = order_res['tk_status']
        self.tk3rd_type = order_res['tk3rd_type']
        if 'tb3rd_pub_id' in order_res:
            self.tk3rd_pub_id = order_res['tk3rd_pub_id']
        self.order_type = order_res['order_type']
        self.income_rate = order_res['income_rate']
        self.pub_share_pre_fee = order_res['pub_share_pre_fee']
        self.subsidy_rate = order_res['subsidy_rate']
        self.subsidy_type = order_res['subsidy_type']
        self.terminal_type = order_res['terminal_type']
        self.auction_category = order_res['auction_category']
        self.site_id = order_res['site_id']
        self.site_name = order_res['site_name']
        self.adzone_id = order_res['adzone_id']
        self.adzone_name = order_res['adzone_name']
        self.alipay_total_price = order_res['alipay_total_price']
        self.total_commission_rate = order_res['total_commission_rate']
        if 'total_commission_fee' in order_res:
            self.total_commission_fee = order_res['total_commission_fee']
        if 'subsidy_fee' in order_res:
            self.subsidy_fee = order_res['subsidy_fee']
        if 'relation_id' in order_res:
            self.relation_id = order_res['relation_id']
        if 'special_id' in order_res:
            self.special_id = order_res['special_id']
        self.click_time = order_res['click_time']
 
class commodity(object):
    def __init__ (self,json_res):
        if 'result' in json_res:
            self.result = json_res['result'] # 1-该产品有优惠券，2-表示该产品无优惠券
            self.category_id = json_res['data']['category_id']
            self.coupon_click_url = json_res['data']['coupon_click_url'] # 
            self.commission_rate = json_res['data']['commission_rate'] #佣金比率 
            self.num_iid = json_res['data']['num_iid']
            self.zk_final_price = json_res['data']['zk_final_price']
            self.volume = json_res['data']['volume'] #最近30天销量
            self.user_type = json_res['data']['user_type']
            self.title = json_res['data']['title']
            self.small_images = json_res['data']['small_images']
            self.seller_id = json_res['data']['seller_id']
            self.reserve_price = json_res['data']['reserve_price'] 
            self.pict_url = json_res['data']['pict_url'] #主图
            self.nick = json_res['data']['nick']
            self.item_url = json_res['data']['item_url']
            self.cat_name = json_res['data']['cat_name']
            self.cat_leaf_name = json_res['data']['cat_leaf_name']
            self.tbk_pwd = json_res['data']['tbk_pwd']
            self.coupon_short_url = json_res['data']['coupon_short_url'] 
            if json_res['result'] == 1: # result:1 表示有优惠券 0 无优惠券
                self.coupon_end_time = json_res['data']['coupon_end_time'] 
                self.coupon_info = json_res['data']['coupon_info'] #折扣信息
                self.coupon_remain_count = json_res['data']['coupon_remain_count'] #剩余优惠券数量，当为0时优惠券失效。
                self.coupon_start_time = json_res['data']['coupon_start_time']
                self.coupon_total_count = json_res['data']['coupon_total_count'] #优惠券发放总量
                self.coupon_type = json_res['data']['coupon_type'] #优惠券类型：1 公开券，2 私有券，3 妈妈券
        else:
            if  'num_iid' in json_res:
                self.result = 3#宝贝已经下架 
                self.msg = json_res['msg']
            else:
                self.result = 4 #不能转换
                self.msg = json_res['msg']
